import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-06-25
 * Time: 21:52
 */
//https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId=13&tqId=11174&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
    //输入两个整数序列，第一个序列表示栈的压入顺序，请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。
public class Test {
    public static boolean IsPopOrder (int[] arr, int[] brr) {
        Stack<Integer> stack=new Stack();
        int i=0;
        int j=0;
        while(i<arr.length){
            if(stack.isEmpty()||stack.peek()!=brr[j]){
                stack.push(arr[i]);
                i++;
            }
            else{
                stack.pop();
                j++;
            }
        }
        while(j< brr.length){
            if(stack.peek()==brr[j]){
                stack.pop();
            }
            j++;
        }
        if(stack.isEmpty()){
            return true;
        }
        else{
            return false;
        }
    }
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5};
        int[] brr={4,5,3,1,2};
        System.out.println(IsPopOrder(arr,brr));
        int[] crr={1,2,3,4,5};
        int[] drr={4,5,3,2,1};
        System.out.println(IsPopOrder(crr,drr));

    }
}
